from time import sleep

from selenium import webdriver
from selenium.webdriver.common.by import By


class Mail163:
    username = "15671812309"
    password = ""

    def __init__(self):
        self.chrome = webdriver.Chrome()
        self.chrome.get("https://mail.163.com")
        self.chrome.maximize_window()
        sleep(2)

    def login(self):
        """
        登录邮箱
        """
        # 切换到frame
        frame = self.chrome.find_element(By.XPATH, "//iframe[1]")
        self.chrome.switch_to.frame(frame)
        # 定位到邮箱输入框
        self.chrome.find_element(By.NAME, "email").send_keys(self.username)
        # 定位到密码输入框
        self.chrome.find_element(By.NAME, "password").send_keys(self.password)
        sleep(2)
        # 定位到登录按钮
        self.chrome.find_element(By.ID, "dologin").click()
        sleep(10)

    def register(self):
        """
        注册邮箱
        """
        self.chrome.find_element(By.PARTIAL_LINK_TEXT, "注册新账号").click()
        sleep(2)
        self.chrome.switch_to.window(self.chrome.window_handles[1])
        sleep(1)
        # 定位到手机号输入框
        self.chrome.find_element(By.NAME, "phone").send_keys(self.username)
        # 定位到密码输入框
        self.chrome.find_element(By.NAME, "password").send_keys(self.password)
        sleep(10)
        # TODO 这里需要手机扫码发送短信验证
        # 勾选同意协议
        # self.chrome.find_element(By.NAME, "agreed").click()
        # 点击注册按钮
        # self.chrome.find_element(By.XPATH, "//button[1]").click()


if __name__ == '__main__':
    mail = Mail163()
    # mail.register()
    mail.login()
